<?php

/**
 * 主控制器
 *
 *
 * @package  app
 * @extends  Controller
 */
class Controller_Game  extends Controller_BaseController {

    public $template = 'template';

    /**
     * 默认方法
     *
     * @access  public
     * @return  \Response
     */
    public function action_index(){

    }

    /**
    * 分类部分
    * @param $cname 栏目名称
    */
    public function action_filter($cname = 'online'){

        if($cname == 'online'){
            $params = array(
                'cats' => array('卡牌', '策略', 'RPG网游', '动作', '三国', '经营', '仙侠'),
                'tags' => array(
                    array('label' => '即时战斗', 'tag' => 'MMO'),
                    array('label' => '卡牌回合', 'tag' => '卡牌,回合制'),
                    array('label' => '经营', 'tag' => '经营'),
                    array('label' => '梦回三国', 'tag' => '三国'),
                    array('label' => '战争题材', 'tag' => '战争'),
                    array('label' => '疯狂飚车', 'tag' => '赛车')
                ),
                'category' => $cname
            );
        }else if($cname == 'single'){
            $params = array(
                'cats' => array('角色扮演', '休闲益智', '动作冒险', '体育竞速', '飞行射击', '经营策略', '棋牌天地', '精品单机'),
                'tags' => array(
                    array('label' => '斗地主', 'tag' => '斗地主'), 
                    array('label' => '美女一起玩', 'tag' => '美女'),
                    array('label' => '恶搞', 'tag' => '恶搞,发泄'),
                    array('label' => '打飞机', 'tag' => '空战'),
                    array('label' => '儿童启蒙', 'tag' => '儿童,亲子'),
                    array('label' => '僵尸游戏', 'tag' => '僵尸'),
                    array('label' => '大家来找茬', 'tag' => '找茬,找不同'),
                    array('label' => '塔防', 'tag' => '塔防'),
                    array('label' => '休闲', 'tag' => '休闲'),
                    array('label' => '动作跑酷', 'tag' => '跑酷'),
                    array('label' => '三国杀', 'tag' => '三国杀,桌游'),
                    array('label' => '麻将', 'tag' => '麻将')
                ),
                'category' => $cname
            );
        }
        

        return \Response::forge(\View::forge('games/filter/filter', $params));
    }

    /**
    * 首页精品推荐
    * @param $cname 栏目名称 
    * @return Response
    */
    public function action_choice_recommend($cname = 'online'){
        $where = array();

        $where['tag'] = array('like', '%精品%');

        $apps = \Model_App::query();
        foreach ($where as $key => $value) {
            if(is_array($value)){
                $apps->where($key, $value[0], $value[1]);
            }else{
                $apps->where($key, $value);
            }            
        }

        $items = $apps->limit(24)->get();

        return \Response::forge(\View::forge('games/home/recommend', array('items' => $items)));
    }

    /**
    * 首页热门精选
    * @param $cname 栏目名称 
    * @return Response
    */
    public function action_choice_hot($cname = 'online'){
        $params = array();

        $where = array();
        $order_by = array();

        if($cname == 'online'){
            $params['class'] = 'hot_recom_div';
            $params['tit_class'] = 'bacff6';
            $params['category'] = '热门网游';
            $where['categoryName'] = array('like', '%游戏:网络游戏%');
        }else if($cname == 'single'){
            $params['class'] = 'hot_recom_div hot_recom_divr';
            $params['tit_class'] = 'bacffc';
            $params['category'] = '精品单机';
            $where['tag'] = array('like', '%精品单机%');
        }else if($cname == 'gospel'){
            $params['class'] = 'hot_recom_div hot_recom_divs';
            $params['tit_class'] = 'bac30c';
            $params['category'] = '高分攻略';
            $where['categoryName'] = '游戏:模拟辅助';
            $where['tag'] = array('like', '%高分攻略%');
            $order_by = array('downloadTimes' => 'desc');
        }else if($cname == 'author'){
            $params['class'] = 'hot_recom_div hot_recom_divs hot_recom_divr';
            $params['tit_class'] = 'bac359';
            $params['category'] = '小编精选';
            $where['tag'] = array('like', '%小编精选%');
        }

        $apps = \Model_App::query();

        //组合条件
        foreach ($where as $key => $value) {
            if(is_array($value)){
                $apps->where($key, $value[0], $value[1]);
            }else{
                $apps->where($key, $value);
            }            
        }

        //组合排序
        if($order_by){
            foreach ($order_by as $key => $value) {
                if(is_numeric($key)){
                    $apps->order_by($value);
                }else{
                    $apps->order_by($key, $value);
                }               
            }
        }

        $params['items'] = $apps->limit(4)->get();

        return \Response::forge(\View::forge('games/home/hot_game', $params));
    }

    /**
    * 首页排行榜
    * @param $cname 排行榜类型 
    * @return Response
    */
    public function action_rank($cname = 'online'){
        $params = array(
            'class' => 'rank_list pl10'
        );

        $where = array();
        $order_by = array('downloadTimes' => 'desc');

        if($cname == 'hot'){
            $params['class'] = 'rank_list';
            $params['tit_class'] = 'bac30c';
            $params['category'] = '热门榜';
            $apps = \Model_AppProperty::getItems('*', array('category_id' => 6), array('app'));
        }else if($cname == 'up'){
            $params['tit_class'] = 'bacffc';
            $params['category'] = '飙升榜';
            $apps = \Model_AppProperty::getItems('*', array('category_id' => 7), array('app'));
        }else if($cname == 'download'){
            $params['tit_class'] = 'bac30c';
            $params['category'] = '下载榜';
            $apps = \Model_AppProperty::getItems('*', array('category_id' => 8), array('app'));
        }else if($cname == 'author'){
            $params['class'] = 'rank_list pl10 r_l_last';
            $params['tit_class'] = 'bac359';
            $params['category'] = '小编推荐';
            $apps = \Model_AppProperty::getItems('*', array('category_id' => 9), array('app'));
        }

        $params['items'] = $apps;

        return \Response::forge(\View::forge('games/home/rank', $params));
    }

    /**
    * 推荐部分
    * @param $cname 栏目名称 
    * @return Response
    */
    public function action_recommend($cname = 'online'){
        $where = array();

        if($cname == 'online'){
            $where['categoryName'] = array('like', '%游戏:网络游戏%');
        }else if($cname == 'single'){
            $where['tag'] = array('like', '%精品单机%');
        }

        $apps = \Model_App::query();
        foreach ($where as $key => $value) {
            if(is_array($value)){
                $apps->where($key, $value[0], $value[1]);
            }else{
                $apps->where($key, $value);
            }            
        }

        $items = $apps->limit(10)->get();

        return \Response::forge(\View::forge('games/filter/recommend', array('recommends' => $items)));
    }

    /**
    * 获取游戏排行部分
    * @param $category 排行分类
    * @return Response
    */
    public function action_game_top($category = 'new'){
        $params = array(
            'class' => $category
        );

        if($category == 'new'){
            $params['title'] = '新热';
            $apps = \Model_AppProperty::getItems('*', array('category_id' => 10), array('app'));
        }else if($category == 'up'){
            $params['title'] = '飙升';
            $apps = \Model_AppProperty::getItems('*', array('category_id' => 11), array('app'));
        }else if($category == 'header'){
            $params['title'] = '权威';
            $apps = \Model_AppProperty::getItems('*', array('category_id' => 12), array('app'));
        }

        $params['items'] = $apps;
        return \Response::forge(\View::forge('games/home/game_top', $params));
    }

    /**
    * 查看一个游戏详细信息
    *
    * @param $id 游戏ID
    * @return 
    */
    public function action_view($id = 0){
        $params = array(
            'title' => ''
        );

        $params['item'] = \Model_App::find($id);
        
        \View::set_global($params);

        $this->template->content = \View::forge('games/details');
    }
}